*/
#include <QtCore/QRegExp>
+#include <QtCore/QTextStream>
#include "defs.h"
-#include "cet_util.h"
#include "csv_util.h"
#include "garmin_fs.h"
#include "grtcirc.h"
xcsv_file.badchars = QString();
xcsv_file.ifield_ct = 0;
xcsv_file.ofield_ct = 0;
- xcsv_file.xcsvfp = NULL;
+ xcsv_file.file = NULL;
+ xcsv_file.stream = NULL;
+ xcsv_file.codec = NULL;
xcsv_file.fname = QString();
xcsv_file.description = NULL;
xcsv_file.extension = NULL;
}
}
+// TODO: eliminate this routine which is modeled
+// after gbfgetstr for legacy compatibility.
+static char*
+xcsv_readline(char* buff)
+{
+ if (buff) {
+ xfree(buff);
+ }
+ QString line = xcsv_file.stream->readLine();
+ if (line.isNull()) {
+ return NULL;
+ } else {
+ // TODO: move csv processing to Qt, eliminating the need to go
+ // back to 8 bit encoding, which is shaky for encoding like utf8
+ // that have multibyte characters.
+ char* newbuff = xstrdup(CSTR(line));
+ return newbuff;
+ }
+}
+
/*****************************************************************************/
/* xcsv_data_read() - read input file, parsing lines, fields and handling */
/* any data conversion (the input meat) */
void
xcsv_data_read(void)
{
- char* buff;
+ char* buff = NULL;
char* s;
Waypoint* wpt_tmp;
int linecount = 0;
csv_route = rte;
}
- while ((buff = gbfgetstr(xcsv_file.xcsvfp))) {
- if ((linecount == 0) && xcsv_file.xcsvfp->unicode) {
- cet_convert_init(CET_CHARSET_UTF8, 1);
- }
-
+ // TODO: stop the back and forth between QString and char strings,
+ while ((buff = xcsv_readline(buff))) {
linecount++;
/* Whack trailing space; leading space may matter if our field sep
* is whitespace and we have leading whitespace.
fmp = (field_map_t*) elem;
if ((i != 0) && !(fmp->options & OPTIONS_NODELIM)) {
- gbfputs(write_delimiter, xcsv_file.xcsvfp);
+ *xcsv_file.stream << write_delimiter;
}
if (fmp->options & OPTIONS_ABSOLUTE) {
if (!xcsv_file.field_encloser.isEmpty()) {
/* print the enclosing character(s) */
- gbfputs(xcsv_file.record_delimiter, xcsv_file.xcsvfp);
+ *xcsv_file.stream << xcsv_file.record_delimiter;
}
/* As a special case (pronounced "horrible hack") we allow
if (0 == strcmp(fmp->printfc, "\"%s\"")) {
obuff = '"' + obuff + '"';
}
- gbfputs(obuff, xcsv_file.xcsvfp);
+ *xcsv_file.stream << obuff;
if (!xcsv_file.field_encloser.isEmpty()) {
/* print the enclosing character(s) */
- gbfputs(xcsv_file.record_delimiter, xcsv_file.xcsvfp);
+ *xcsv_file.stream << xcsv_file.record_delimiter;
}
buff.clear();
}
- gbfputs(xcsv_file.record_delimiter, xcsv_file.xcsvfp);
+ *xcsv_file.stream << xcsv_file.record_delimiter;
/* increment the index counter */
waypt_out_count++;
QString t = dt.toString("hh:mm:ss");
cout.replace("__TIME__", t);
}
- gbfputs(cout, xcsv_file.xcsvfp);
- gbfputs(xcsv_file.record_delimiter, xcsv_file.xcsvfp);
+ *xcsv_file.stream << cout << xcsv_file.record_delimiter;
}
if ((xcsv_file.datatype == 0) || (xcsv_file.datatype == wptdata)) {
/* output epilogue lines, if any. */
foreach(const QString& ogp, xcsv_file.epilogue) {
- gbfputs(ogp, xcsv_file.xcsvfp);
- gbfputs(xcsv_file.record_delimiter, xcsv_file.xcsvfp);
+ *xcsv_file.stream << ogp << xcsv_file.record_delimiter;
}
}
#endif
*/
+#include <QtCore/QTextCodec>
+#include <QtCore/QTextStream>
+
#include "defs.h"
-#include "cet_util.h"
#include "csv_util.h"
#include "jeeps/gpsmath.h"
+#include "src/core/file.h"
+#include "src/core/logging.h"
#include <ctype.h>
#include <stdlib.h>
if (ISSTOKEN(sbuff, "ENCODING")) {
p = csv_stringtrim(&sbuff[8], "\"", 1);
- cet_convert_init(p, 1);
+ xcsv_file.codec = QTextCodec::codecForName(p);
+ if (!xcsv_file.codec) {
+ Fatal() << "Unsupported character set '" << p << "'.";
+ }
xfree(p);
} else
}
}
- xcsv_file.xcsvfp = gbfopen(fname, "r", MYNAME);
+ xcsv_file.file = new gpsbabel::File(fname);
+ xcsv_file.file->open(QFile::ReadOnly);
+ xcsv_file.stream = new QTextStream(xcsv_file.file);
+ if (xcsv_file.codec) {
+ xcsv_file.stream->setCodec(xcsv_file.codec);
+ } else {
+ // default to UTF-8.
+ xcsv_file.stream->setCodec("UTF-8");
+ xcsv_file.stream->setAutoDetectUnicode(true);
+ }
xcsv_file.gps_datum = GPS_Lookup_Datum_Index(opt_datum);
is_fatal(xcsv_file.gps_datum < 0, MYNAME ": datum \"%s\" is not supported.", opt_datum);
}
static void
xcsv_rd_deinit(void)
{
- gbfclose(xcsv_file.xcsvfp);
+ xcsv_file.file->close();
+ delete xcsv_file.file;
+ xcsv_file.file = NULL;
+ delete xcsv_file.stream;
+ xcsv_file.stream = NULL;
+ xcsv_file.codec = NULL;
xcsv_destroy_style();
}
xcsv_read_style(styleopt);
}
- xcsv_file.xcsvfp = gbfopen(fname, "w", MYNAME);
+ xcsv_file.file = new gpsbabel::File(fname);
+ xcsv_file.file->open(QFile::WriteOnly | QFile::Text);
+ xcsv_file.stream = new QTextStream(xcsv_file.file);
+ if (xcsv_file.codec) {
+ xcsv_file.stream->setCodec(xcsv_file.codec);
+ // enable bom for all UTF codecs except UTF-8
+ if (xcsv_file.codec->mibEnum() != 106) {
+ xcsv_file.stream->setGenerateByteOrderMark(true);
+ }
+ } else {
+ // emulate gbfputs which assumes UTF-8.
+ xcsv_file.stream->setCodec("UTF-8");
+ }
xcsv_file.fname = fname;
/* set mkshort options from the command line */
static void
xcsv_wr_deinit(void)
{
- gbfclose(xcsv_file.xcsvfp);
+ xcsv_file.stream->flush();
+ xcsv_file.file->close();
+ delete xcsv_file.file;
+ xcsv_file.file = NULL;
+ delete xcsv_file.stream;
+ xcsv_file.stream = NULL;
+ xcsv_file.codec = NULL;
xcsv_destroy_style();
}
xcsv_data_write();
waypt_del(wpt);
- gbfflush(xcsv_file.xcsvfp);
+ xcsv_file.stream->flush();
}
ff_vecs_t xcsv_vecs = {